package com.yonyou.wdt.utils;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.yonyou.wdt.entity.QueryDto;
import com.yonyou.wdt.service.YonsuiteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @ClassName WarhouseQueryUtil
 * @Description
 * @Author HuangWb
 * @Date 2023/5/19 11:54
 * @Version 1.0
 */
@Component
public class YsQueryUtil {

    private static final Logger logger = LoggerFactory.getLogger(YsQueryUtil.class);

    /**
     * @return
     * @Author Huangwb
     * @Description 查询ys仓库档案数据
     * @Date 11:53 2023/5/19
     * @Param
     **/
    public static HashMap<String, String> getCkData(QueryDto dto,YonsuiteService yonsuiteService) throws Exception {
        // 1、仓库列表查询，查询出所有仓库，将id与code拼装成一个map集合，进行比对判断
        HashMap<String, Object> queryMap = new HashMap<>();  // 仓库档案列表查询请求参数Map
        ArrayList<JSONObject> warehouseList = new ArrayList<>();  // 仓库档案列表查询返回值
        int pageIndex = 1;
        while (true) {
            queryMap.put("pageIndex", pageIndex);
            queryMap.put("pageSize", 100);
            JSONObject wareJsonList = yonsuiteService.warehouseQuery(queryMap, dto);
            if (wareJsonList.getStr("code").equals("200")) {
                if (null != wareJsonList.getJSONObject("data") && null != wareJsonList.getJSONObject("data").getJSONArray("recordList")) {
                    JSONArray purJSONArray = wareJsonList.getJSONObject("data").getJSONArray("recordList");
                    for (Object warehouse : purJSONArray) {
                        JSONObject warehouseJson = (JSONObject) warehouse;
                        warehouseList.add(warehouseJson);
                    }
                }
            }
            if (wareJsonList.getJSONObject("data").getInt("pageCount") >= pageIndex) {
                break;
            }
            pageIndex++;
        }
        logger.info("YS系统仓库档案查询完成共：{}个仓库", warehouseList.size());
        HashMap<String, String> warehouseMap = new HashMap<>();
        for (JSONObject warehouseJson : warehouseList) {
            warehouseMap.put(warehouseJson.getStr("id"), warehouseJson.getStr("code"));
        }
        return warehouseMap;
    }

    /**
     * @return
     * @Author Huangwb
     * @Description 查询ys仓库档案数据
     * @Date 11:53 2023/5/19
     * @Param
     **/
    public static HashMap<String, String> getFhdpDef(QueryDto dto,YonsuiteService yonsuiteService) throws Exception {
        // 1、旺店通发货店铺列表查询，查询出所有旺店通发货店铺，将id与code拼装成一个map集合，进行比对判断
        HashMap<String, Object> queryMap = new HashMap<>();  // 仓库档案列表查询请求参数Map
        ArrayList<JSONObject> custList = new ArrayList<>();  // 仓库档案列表查询返回值
        int pageIndex = 1;
        while (true) {
            queryMap.put("pageIndex", pageIndex);
            queryMap.put("pageSize", 100);
            queryMap.put("custdocdefcode", "maoer01");
            JSONObject custDocJsonList = yonsuiteService.custDocQuery(queryMap, dto);
            if (custDocJsonList.getStr("code").equals("200")) {
                if (null != custDocJsonList.getJSONObject("data") && null != custDocJsonList.getJSONObject("data").getJSONArray("recordList")) {
                    JSONArray purJSONArray = custDocJsonList.getJSONObject("data").getJSONArray("recordList");
                    for (Object warehouse : purJSONArray) {
                        JSONObject warehouseJson = (JSONObject) warehouse;
                        custList.add(warehouseJson);
                    }
                }
            }
            if (custDocJsonList.getJSONObject("data").getInt("pageCount") >= pageIndex) {
                break;
            }
            pageIndex++;
        }
        logger.info("YS系统旺店通发货店铺查询完成共：{}个", custList.size());
        HashMap<String, String> custMap = new HashMap<>();
        for (JSONObject custJson : custList) {
            custMap.put(custJson.getStr("id"), custJson.getStr("code"));
        }
        return custMap;
    }

    /**
     * @return
     * @Author Huangwb
     * @Description 查询ys客户档案数据
     * @Date 11:53 2023/5/19
     * @Param
     **/
    public static HashMap<String, String> getCsutData(QueryDto dto, YonsuiteService yonsuiteService) throws Exception {
        // 1、客户列表查询，查询出所有客户，将id与code拼装成一个map集合，进行比对判断
        HashMap<String, Object> queryMap = new HashMap<>();  // 客户档案列表查询请求参数Map
        ArrayList<JSONObject> warehouseList = new ArrayList<>();  // 客户档案列表查询返回值
        int pageIndex = 1;
        while (true) {
            queryMap.put("pageIndex", pageIndex);
            queryMap.put("pageSize", 500);
            JSONObject wareJsonList = yonsuiteService.getMerchantList(queryMap, dto);
            if (wareJsonList.getStr("code").equals("200")) {
                if (null != wareJsonList.getJSONArray("data")) {
                    JSONArray purJSONArray = wareJsonList.getJSONArray("data");
                    for (Object warehouse : purJSONArray) {
                        JSONObject warehouseJson = (JSONObject) warehouse;
                        warehouseList.add(warehouseJson);
                    }
                }
            }else {
                break;
            }
            pageIndex++;
        }
        logger.info("YS系统客户档案查询完成共：{}个客户", warehouseList.size());
        HashMap<String, String> warehouseMap = new HashMap<>();
        for (JSONObject warehouseJson : warehouseList) {
            warehouseMap.put(warehouseJson.getStr("id"), warehouseJson.getStr("code"));
        }
        return warehouseMap;
    }
}
